//变量
var code = document.getElementById('playJs').getAttribute('code');
var video_cdn1 = document.getElementById('playJs').getAttribute('cdn1');
var video_cdn2 = document.getElementById('playJs').getAttribute('cdn2');
var video_path =  document.getElementById('playJs').getAttribute('videopath');
var video_shareid =  document.getElementById('playJs').getAttribute('shareid');
var videoid =  document.getElementById('playJs').getAttribute('videoid');
//图片加载方式
var pic_type = document.getElementById('playJs').getAttribute('pic_type');

var meuns = ['返回首页', '投诉'];
var cancel = '关闭菜单';

//顶部右上角菜单
hui('#hui-header-menu').click(function(){
    hui.actionSheet(meuns, cancel, function(e){
        if(e.index==0){
            location.replace('/born?code='+code)
        }
        else if(e.index==1){
            location.href=('/tousu/index.html?code='+code)
        }
    });
});

//获取落地域名
function goborn(){
    location.href="/born?code="+code
}

//已经上报的图片地址记录
errImgs = new Array()
//上报图片加载错误
function reportImgErr(path){
    if(errImgs.length > 0){
        hui.postJSON('/api/img-error',
            {
                code:code,
                path:path
            },
            function(res){
                if(res.status=='success'){
                }
            }
        )
    }
}

var tags =[]
var tag_meuns = [];
hui('#comment').click(function(){
    hui.actionSheet(tag_meuns, '取消', function(e){
        console.log(e.index);
        for (let item of tags) {
            if(item['name']==e.innerHTML){
                hui.ajax({
                    type : 'POST',
                    url : '/api/tag-to-video',
                    data : {
                        code:code,
                        video_id:videoid,
                        tag_id:item['id']
                    },
                    timeout:10000,
                    backType:'JSON',
                    beforeSend : function(){ 
                        hui.loading('正在提交...');
                    },
                    complete   : function(){
                        hui.closeLoading();
                    },
                    success : function(res){
                        if(res['status'] == 'success'){
                            hui.iconToast('提交成功,感谢您的反馈', 'success')
                        }else{
                        }
                    },
                    error : function(e){
                        hui.upToast('网络异常，请重试！');
                    }
                })
                break;
            }
        }
    });
});
hui.ajax({
    type : 'POST',
    url : '/api/tags',
    data : {
        code:code,
    },
    timeout:10000,
    backType:'JSON',
    beforeSend : function(){},
    complete   : function(){},
    success : function(res){
        if(res['status'] == 'success'){
            tags = res['data']['tags'];
            for (let i of tags) {
                tag_meuns.push(i['name'])
            }
        }else{
        }
    },
    error : function(e){
    }
})
//加载推荐视频
function loadvideo(){
    page = 1
    hui.postJSON('/api/videos',
        {
            code:code,
            recommend:1,
            random_view:1,
            page:1,
            limit:16,
            pic_type:pic_type
        },
        function(res){
            hui.closeLoading()
            if(res['status'] == 'success'){
                document.getElementById('video_list').innerHTML = ""
                appendData(res['data']['videos'],pic_type,code,res['data']['img_cdns']||[],res['data']['img_domain']||'');
                page++
            }else{
                hui.iconToast('加载失败，请重试', 'warn')
            }
        },
        function(){
            hui.upToast('网络异常，请重试！');
        }
    )
}

//上报图片或视频错误
function reportUrlErr(path,type){
    if(type==1 && errImgs.length > 0){
        hui.postJSON('/api/url-error',
            {
                code:code,
                path:path,
                type:type
            },
            function(res){
            }
        )
    }else{
        hui.postJSON('/api/url-error',
            {
                code:code,
                path:path,
                type:type
            },
            function(res){
            }
        )
    }
}
//url判断
function NetPing(path,type) {
    $.ajax({
        type: "GET",
        cache: false,
        url: path,
        data: "",
        success: function() {
        },
        error: function() {
            reportUrlErr(path,type)
        }
    });
}

//获取视频cdn
function getVideoCdns(){
    return new Promise(function(resolve) {
        hui.ajax({
            type : 'POST',
            url : '/api/video-cdns',
            data : {
                code:code,
                recommend:1,
                page:1,
                limit:16,
            },
            timeout:10000,
            backType:'JSON',
            beforeSend : function(){hui.loading('切换中...')},
            complete   : function(){
                hui.closeLoading()
            },
            success : function(res){
                if(res['status'] == 'success'){
                    let cdns = res['data']['video_cdns']
                    video_cdn1 = cdns["video_cdn1"]['domain']
                    video_cdn2 = cdns["video_cdn2"]['domain']
                    resolve()
                }else{
                    hui.iconToast('切换失败，可尝试刷新当前页面', 'warn')
                }
            },
            error : function(e){
                hui.upToast('连接服务器失败！')
            }
        })
    })
}


window.onload=function(){
    //====================== ckplay 直接播放m3u8 ===================
    video_p = video_path.replace('index.m3u8', "10000kb/hls/index.m3u8")
    let video_url = "http://"+video_cdn1+"/"+video_p
    //初始化播放器 定义变量：videoObject，用来做为视频初始化配置
    var videoObject = {
        container: '.video-player', //“#”代表容器的ID，“.”或“”代表容器的class
        variable: 'player', //播放函数名称，该属性必需设置，值等于下面的new ckplayer()的对象
        video: video_url,//视频地址
        // autoplay:true,
        // type:'video/m3u8',
        poster: 'https://static-js001.oss-cn-guangzhou.aliyuncs.com/images/play.jpg?versionId=CAEQDBiBgICJpayizRciIDY1YTAyMjc2OGU0NTQ0ZWVhMjRmNmVlYjc2NzVjZTdi',
    };
    var player = new ckplayer(videoObject);//初始化播放器


     //切换播放器
     function playLine(line){
        getVideoCdns().then(function(){
            if(line==1){
                player.newVideo({autoplay:true,video:"http://"+video_cdn1+"/"+video_p});
            }
            if(line==2){
                player.newVideo({autoplay:true,video:"http://"+video_cdn2+"/"+video_p});
            }
            hui.upToast('线路已切换，点击播放按钮播放')
        })
    }
    //========================= ckplay 直接播放m3u8 结束 ===============================



    //=================== ppvod 播放 =======================================
    // let video_url = "http://"+video_cdn1+"/share/"+video_shareid
    // document.getElementById('video-play').src = video_url

    // function playLine(line){
    //     getVideoCdns().then(function(){
    //         if(line==1){
    //             document.getElementById('video-play').src =  "http://"+video_cdn1+"/share/"+video_shareid
    //         }
    //         if(line==2){
    //             document.getElementById('video-play').src =  "http://"+video_cdn2+"/share/"+video_shareid
    //         }
    //         hui.upToast('线路已切换，点击播放按钮播放')
    //     })
    // }
    //======================= ppvod 播放 结束 =================================
   

   

    //切换线路播放
    hui('#line01').click(function(){
        playLine(1)
        $('.current_line').text('当前【线路一】')
    })
    hui('#line02').click(function(){
        playLine(2)
        $('.current_line').text('当前【线路二】')
    })
    //加载推荐视频
    loadvideo()


    //检测视频链接
    // NetPing("http://"+video_cdn1+"/"+video_url,2)
    // NetPing("http://"+video_cdn2+"/"+video_url,2)
}
